<?php


namespace App\Http\Controllers\Index;


use App\Http\Controllers\Controller;
use App\Model\Banner;
use App\Model\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class IndexController extends Controller
{
    protected $param;
    public function __construct(Request $request)
    {
        if ($request->isMethod('GET')){
            $this->param = json_decode($request->input('params'),true);
        }elseif ($request->isMethod('POST')){
            $this->param = $request->input('params');
        }
    }

    /**
     * 获取首页banner
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function banner()
    {
        $banners = Banner::all();
        return JsonResponse(200,true,'获取成功',getImageUrl($banners,'url'));
    }

    /**
     * 获取首页推荐产品
     * @return \Illuminate\Http\JsonResponse
     */
    public function home_product(){
        $MProduct = new Product();
        $list = $MProduct->getHomeProduct($this->param);
        return JsonResponse(200,true,'获取成功',getImageUrl($list,'main_image'));
    }

    /**
     * 获取首页推荐分类
     * @return \Illuminate\Http\JsonResponse
     */
    public function category(Request $request){
        $categories = DB::table('category')
            ->where('is_home',$this->param['is_home'])
            ->orderBy('id', 'asc')
            ->get();
        return JsonResponse(200,true,'获取成功',getImageUrl($categories,'icon'),$request->input());
    }

    /**
     * 获取首页热门分类icon
     * @return \Illuminate\Http\JsonResponse
     */
    public function home_icon(Request $request){
        $categories = DB::table('category')
            ->where('is_home',$this->param['is_home'])
            ->orderBy('id', 'asc')
            ->get();
        return JsonResponse(200,true,'获取成功',getImageUrl($categories,'icon'),$this->param);
    }
}
